<template>
	<a-drawer
		:title="formData.id ? '编辑' : '增加'"
		:width="500"
		:visible="visible"
		:destroy-on-close="true"
		:body-style="{ paddingBottom: '80px' }"
		:footer-style="{ textAlign: 'right' }"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-form-item label="卡类型：" name="cardType">
				<a-select
					:disabled="readonly"
					v-model:value="formData.cardType"
					:options="cardTypeOptions"
					placeholder="请选择卡类型"></a-select>
			</a-form-item>
			<a-form-item label="发卡门店：" name="cardType">
				<a-select
					:disabled="readonly"
					v-model:value="formData.storeCode"
					:options="orgOptions"
					placeholder="请选择门店"></a-select>
			</a-form-item>
			<a-form-item label="发卡数量：" name="totalCount">
				<a-input-number style="width: 160px" :disabled="readonly" v-model:value="formData.totalCount" :min="1" placeholder="请输入发卡数量" allow-clear />
			</a-form-item>
			<a-form-item label="自定义卡号：">
				<a-switch  v-model:checked="formData.isCustom"/>
			</a-form-item>
			<template v-if="formData.isCustom">
				<a-form-item label="卡号前缀：" name="prefix">
					<a-input :disabled="readonly" v-model:value="formData.prefix" :min="1" placeholder="请输入卡号前缀" allow-clear />
				</a-form-item>
				<a-form-item label="起始序号：" name="startNo">
					<a-input-number style="width: 160px" :disabled="readonly" v-model:value="formData.startNo" :min="1" placeholder="请输入起始序号" allow-clear />
				</a-form-item>
			</template>

			<a-form-item label="备注：" name="note">
				<a-textarea :disabled="readonly" v-model:value="formData.note" placeholder="请输入备注" allow-clear />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" :loading="submitLoading" @click="onSubmit">保存</a-button>
		</template>
		<Icon-selector ref="iconSelector" @iconCallBack="iconCallBack" />
	</a-drawer>
</template>

<script setup name="createCardForm">
	import { message } from 'ant-design-vue'
	import {required} from '@/utils/formRules'
	import IconSelector from '@/components/Selector/iconSelector.vue'
	import crmApi from '@/api/crm/crmApi'
	import { getCurrentInstance } from 'vue'
	// 默认是关闭状态
	let visible = $ref(false)
	let readonly = $ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	const formData = ref({})
	const submitLoading = ref(false)
	const iconSelector = ref()
	const customCardNo = ref(false)

	const { proxy } = getCurrentInstance()

	// 打开抽屉
	const onOpen = (record, _readonly = false) => {
		visible = true
		readonly = _readonly
		formData.value = {
			totalCount: 1
		}
		if (record) {
			formData.value = Object.assign({}, record)
			formData.isCustom = formData.customNo === 1
			let _cardTypeOptions = proxy.$TOOL
				.tableList('CRM_CARD_TYPE')
				.map((item) => {
					return {
						prop: item['cardProp'],
						value: item['id'],
						label: item['name']
					}
				})
			_cardTypeOptions = _cardTypeOptions.filter(item => {
				return item.prop === formData.value.cardProp
			})
			cardTypeOptions.length = 0
			_cardTypeOptions.forEach(item => {
				cardTypeOptions.push(item)
			})
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		visible = false
	}

	// 默认要校验的
	const formRules = {
		cardType: [required('请选择卡类型')],
		totalCount: [required('请输入发卡数量')],
	}

	// 图标选择器回调
	const iconCallBack = (value) => {
		formData.value.icon = value
	}

	let cardTypeOptions = proxy.$TOOL
		.tableList('CRM_CARD_TYPE')
		.map((item) => {
			return {
				prop: item['cardProp'],
				value: item['id'],
				label: item['name']
			}
		})

	let orgOptions = proxy.$TOOL
		.tableList("ORG_CATEGORY")
		.map((item) => {
			return {
				value: item['code'],
				label: item['name']
			}
		})

	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				const param = parameterChanges(formData.value)
				if (param.customCardNo && !param.prefix) {
					message.error('请输入卡号前缀')
				} else if (param.customCardNo && !param.startNo && param.startNo < 1) {
					message.error('开始序号不能小于1')
				} else {
					submitLoading.value = true
					crmApi.cardOrderSubmitForm(param, param.id).then(() => {
						visible = false
						emit('successful')
					}).finally(() => {
						submitLoading.value = false
					})
				}
			})
	}
	const parameterChanges = (data) => {
		if (!data.component) {
			return data
		}
		// 如果用户输入的组件path路径
		if (data.component.slice(0, 1) === '/') {
			data.component = data.component.slice(1)
		}
		return data
	}
	// 调用这个函数将子组件的一些数据和方法暴露出去
	defineExpose({
		onOpen
	})
</script>
